//
//  ShopDetailsViewController.m
//  carowner
//
//  Created by apple on 2019/5/22.
//  Copyright © 2019 ggj. All rights reserved.
//

#import "ShopDetailsViewController.h"
#import "ShopDetailsEntity.h"
#import "PhotoCell.h"
#import "EvaluateCell.h"
#import "CommentListEntity.h"


@interface ShopDetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIWebViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIScrollView * scrollerView;
@property(nonatomic,strong)UIImageView * topImg;
@property(nonatomic,strong)UILabel * contactTip;
@property(nonatomic,strong)UILabel * tvAddress;
@property(nonatomic,strong)UILabel * tvPhone;
@property(nonatomic,strong)UILabel * openTime;
@property(nonatomic,strong)UIButton * btnLoc;
@property(nonatomic,strong)UIButton * btnPhone;
@property(nonatomic,strong)UIView * lineView1;
@property(nonatomic,strong)UIView * lineView2;
@property(nonatomic,strong)UIView * lineView3;
@property(nonatomic,strong)UILabel * shopServiceTip;
@property(nonatomic,strong)UIScrollView * serviceTagView;
@property(nonatomic,strong)UILabel * shopBrandTip;
@property(nonatomic,strong)UICollectionView * brandTableView;
@property(nonatomic,strong)UILabel * comExplain;
@property(nonatomic,strong)UIWebView * explainWebView;
@property(nonatomic,strong)UIView * line4;
@property(nonatomic,strong)UILabel * customerEvaluation;
@property(nonatomic,strong)UILabel * totalEvaluation;
@property(nonatomic,strong)UITableView * dataRyc;
@property(nonatomic,strong)NSArray<NSString *> * brandPicArray;
@property(nonatomic,strong)NSArray * commentArray;


@end

@implementation ShopDetailsViewController


-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"门店信息";
    [self initViews];
    NSDictionary * dct = [[NSDictionary alloc]initWithObjectsAndKeys:_shopId,@"id", nil];
    [[ApiManager sharedManager] getShopDetails:dct andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ReturnObject];
        ShopDetailsEntity * entity = [ShopDetailsEntity mj_objectWithKeyValues:resultData];
        self.title = entity.name;
        [MyUtils loadImageWithUrl:entity.imgsrc intoView:_topImg];
        _tvAddress.text = [NSString stringWithFormat:@"%@%@",@"地址:",entity.address];
        _tvPhone.text = [NSString stringWithFormat:@"%@%@",@"电话:",entity.phone];
        _openTime.text = [NSString stringWithFormat:@"%@%@%@%@",@"营业时间:",entity.openTime,@"-",entity.closeTime];
        
        NSArray<NSString *> * array = entity.serviceProjectList;
        _brandPicArray = entity.brandPicList;
        [_brandTableView reloadData];
        NSString * htmlData = [NSString stringWithFormat:@"<div id='foo'>%@</div>",entity.note];

        [self.explainWebView loadHTMLString:htmlData baseURL:nil];
        
        for(int i = 0 ; i < array.count ;i++){
            UILabel * _tvLabel = [[UILabel alloc]initWithFrame:CGRectMake(50*i, 0, 40, 30)];
            _tvLabel.text = array[i];
            _tvLabel.font = textFont;
            _tvLabel.tag = 10;
            _tvLabel.layer.cornerRadius = 4;
            _tvLabel.layer.masksToBounds = YES;
            _tvLabel.layer.borderColor = [[UIColor redColor] CGColor];
            _tvLabel.layer.borderWidth = 1;
            _tvLabel.textAlignment = NSTextAlignmentCenter;
            _tvLabel.textColor = [UIColor redColor];
            [_serviceTagView addSubview:_tvLabel];
        }
        
    }];
    
    NSDictionary * dt = [[NSDictionary alloc] initWithObjectsAndKeys:_shopId,@"companyId",@"0",@"page",nil];
    [[ApiManager sharedManager] getShopComments:dt andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        _commentArray= [CommentListEntity mj_objectArrayWithKeyValuesArray:resultData];
        if(_commentArray!=nil){
            [_dataRyc mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_totalEvaluation.mas_bottom).offset(10);
                make.left.right.equalTo(self.scrollerView);
                make.size.mas_equalTo(CGSizeMake(kScreen_Width, _commentArray.count*140));
            }];
            [self.view layoutIfNeeded];
            [_dataRyc reloadData];
            self.scrollerView.contentSize =  CGSizeMake(kScreen_Width,kSafeArea_Bottom + _customerEvaluation.frame.origin.y+(_commentArray == nil ? 0: _commentArray.count)*140);
        }
    }];
}

-(void)initViews{
    
    _scrollerView = [[UIScrollView alloc] init];
    _scrollerView.scrollEnabled = YES;
    _scrollerView.bounces = YES;
    [self.view addSubview:_scrollerView];
    
    [_scrollerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.width.mas_equalTo(kScreen_Width);
        make.top.equalTo(self.view).offset(getRectNavAndStatusHight);
    }];
    
    _topImg = [[UIImageView alloc] init];
    _topImg.backgroundColor = gray_color;
    _topImg.contentMode = UIViewContentModeScaleAspectFill;
    _topImg.clipsToBounds = YES;//  是否剪切掉超出 UIImageView 范围的图片[imageView setContentScaleFactor:[[UIScreenmainScreen] scale]];
    _contactTip = [[UILabel alloc] init];
    _contactTip.text = @"联系方式";
    _contactTip.font = textFont;
    
    _tvAddress = [[UILabel alloc] init];
    _tvAddress.text = @"地址:";
    _tvAddress.font = smallFont;
    
    _tvPhone = [[UILabel alloc] init];
    _tvPhone.text = @"联系电话:";
    _tvPhone.font = smallFont;
    
    _openTime = [[UILabel alloc] init];
    _openTime.text = @"营业时间:";
    _openTime.font = smallFont;

    
    _btnLoc = [[UIButton alloc] init];
    [_btnLoc setImage:[UIImage imageNamed:@"loc_icon"] forState:UIControlStateNormal];
    
    _btnPhone = [[UIButton alloc] init];
    [_btnPhone setImage:[UIImage imageNamed:@"phone_icon"] forState:UIControlStateNormal];
    
    _lineView1 = [NSObject getLineView];
    
    _shopServiceTip = [[UILabel alloc] init];
    _shopServiceTip.text = @"店铺服务";
    _shopServiceTip.font = textFont;
    
    
    _serviceTagView = [[UIScrollView alloc] init];
    
    _lineView2 = [NSObject getLineView];
    
    _shopBrandTip = [[UILabel alloc] init];
    _shopBrandTip.font = textFont;
    _shopBrandTip.text = @"店铺品牌授权书";
    
    _lineView3 = [NSObject getLineView];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    _brandTableView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    _brandTableView.delegate = self;
    _brandTableView.dataSource = self;
    _brandTableView.contentInset = UIEdgeInsetsMake(10,0,10,0);
    _brandTableView.backgroundColor = [UIColor clearColor];
    _brandTableView.scrollEnabled = NO;
    
    [_brandTableView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"picId"];
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    
    _comExplain = [[UILabel alloc] init];
    _comExplain.text = @"公司简介";
    _comExplain.font = textFont;
    
    _explainWebView = [[UIWebView alloc] init];
    _explainWebView.delegate = self;
    _explainWebView.scrollView.scrollEnabled = NO;
    
    _line4 = [NSObject getLineView];
    
    _customerEvaluation = [[UILabel alloc] init];
    _customerEvaluation.text = @"客户评价";
    _customerEvaluation.font = textFont;
    

    _totalEvaluation = [[UILabel alloc] init];
    _totalEvaluation.text = @"综合:";
    _totalEvaluation.font = textFont;
    _totalEvaluation.textAlignment = NSTextAlignmentRight;
    
    
    _dataRyc = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _dataRyc.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_dataRyc registerClass:[EvaluateCell class] forCellReuseIdentifier:@"dataID"];
    _dataRyc.delegate = self;
    _dataRyc.dataSource = self;
    
    
    [self.scrollerView addSubview:_topImg];
    [self.scrollerView addSubview:_contactTip];
    [self.scrollerView addSubview:_tvAddress];
    [self.scrollerView addSubview:_tvPhone];
    [self.scrollerView addSubview:_openTime];
    [self.scrollerView addSubview:_btnLoc];
    [self.scrollerView addSubview:_btnPhone];
    [self.scrollerView addSubview:_lineView1];
    [self.scrollerView addSubview:_shopServiceTip];
    [self.scrollerView addSubview:_serviceTagView];
    [self.scrollerView addSubview:_lineView2];
    [self.scrollerView addSubview:_shopBrandTip];
    [self.scrollerView addSubview:_lineView3];
    [self.scrollerView addSubview:_brandTableView];
    [self.scrollerView addSubview:_comExplain];
    [self.scrollerView addSubview:_explainWebView];
    [self.scrollerView addSubview:_line4];
    [self.scrollerView addSubview:_customerEvaluation];
    [self.scrollerView addSubview:_totalEvaluation];
    [self.scrollerView addSubview:_dataRyc];
    
    [_topImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollerView).offset(10);
        make.top.equalTo(self.scrollerView).offset(10);
        make.right.equalTo(self.scrollerView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 160));
    }];
    
    [_contactTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollerView).offset(10);
        make.top.equalTo(_topImg.mas_bottom).offset(30);
        make.width.mas_equalTo(kScreen_Width);
    }];
    
    [_tvAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollerView).offset(10);
        make.top.equalTo(_contactTip.mas_bottom).offset(20);
        make.width.mas_equalTo(kScreen_Width-50);

    }];
    
    [_tvPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollerView).offset(10);
        make.top.equalTo(_tvAddress.mas_bottom).offset(15);
        make.width.mas_equalTo(kScreen_Width-50);
    }];
    
    [_openTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollerView).offset(10);
        make.top.equalTo(_tvPhone.mas_bottom).offset(15);
        make.width.mas_equalTo(kScreen_Width-50);
    }];
    
    [_btnLoc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.scrollerView).offset(-30);
        make.centerY.equalTo(_tvAddress);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [_btnPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.scrollerView.mas_right).offset(-30);
        make.centerY.equalTo(_tvPhone);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [_lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_openTime.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 3));
    }];
    
    [_shopServiceTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_lineView1.mas_bottom).offset(15);
        make.left.equalTo(self.scrollerView).offset(10);
    }];
    
    [_serviceTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_shopServiceTip.mas_bottom).offset(10);
        make.left.equalTo(self.scrollerView).offset(10);
        make.right.equalTo(self.scrollerView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 40));
    }];
    
    [_lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_serviceTagView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 3));
    }];
    
    [_shopBrandTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_lineView2.mas_bottom).offset(15);
        make.left.equalTo(self.scrollerView).offset(10);
    }];
    
    
    [_brandTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_shopBrandTip.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 80));
    }];
    
    
    [_lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_brandTableView.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 3));
    }];
    
    [_comExplain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_lineView3.mas_bottom).offset(15);
        make.left.equalTo(self.scrollerView).offset(10);
    }];
    
    [_explainWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.scrollerView);
        make.top.equalTo(_comExplain.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 300));
    }];
    
    [_line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_explainWebView.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 3));
    }];
    
    [_customerEvaluation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_line4.mas_bottom).offset(15);
        make.left.equalTo(_scrollerView).offset(10);
    }];

    [_totalEvaluation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_line4.mas_bottom).offset(15);
        make.right.equalTo(_scrollerView).offset(-40);
        make.size.mas_equalTo(CGSizeMake(80,30));
    }];
    
    [_dataRyc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_totalEvaluation.mas_bottom).offset(10);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 0));
    }];
    
    [self.view layoutIfNeeded];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.scrollerView.contentSize =  CGSizeMake(kScreen_Width,_explainWebView.frame.origin.y);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _brandPicArray == nil ? 0:_brandPicArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoCell * cell = [_brandTableView dequeueReusableCellWithReuseIdentifier:@"picId" forIndexPath:indexPath];
    [cell setImageUrl:_brandPicArray[indexPath.row]];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreen_Width/4, 80);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
}


-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *script = [NSString stringWithFormat:
                        @"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var img;"
                        "var maxwidth=%f;"
                        "for(i=0;i <document.images.length;i++){"
                        "img = document.images[i];"
                        "if(img.width > maxwidth){"
                        "img.width = maxwidth;"
                        "}"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('head')[0].appendChild(script);", kScreen_Width - 20];
    
    [_explainWebView stringByEvaluatingJavaScriptFromString: script];
    [_explainWebView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    NSString *htmlHeight = [_explainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
    
    [_explainWebView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.scrollerView);
        make.top.equalTo(_comExplain.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, [htmlHeight intValue]));
    }];
    [self.view layoutIfNeeded];
    self.scrollerView.contentSize =  CGSizeMake(kScreen_Width, kSafeArea_Bottom+_customerEvaluation.frame.origin.y+(_commentArray == nil ? 0: _commentArray.count)*140);

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _commentArray == nil?0:_commentArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EvaluateCell * cell = [tableView dequeueReusableCellWithIdentifier:@"dataID"];
    CommentListEntity * entity = [_commentArray objectAtIndex:indexPath.row];
    [MyUtils loadImageWithUrl:entity.path intoView:cell.headImg];
    [MyUtils loadImageWithUrl:entity.cover intoView:cell.carImage];
    cell.tvName.text = entity.nickName;
    cell.tvTime.text = entity.createTime;
    cell.tvComment.text = entity.note;
    cell.serviceStar.text = [NSString stringWithFormat:@"%@%d",@"服务态度评分:",(int)entity.serviceAttitude];
    cell.fixStar.text = [NSString stringWithFormat:@"%@%d",@"维修质量评分:",(int)entity.repairQuality];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
//    return [_dataRyc fd_heightForCellWithIdentifier:@"cellID" configuration:^(EvaluateCell * cell) {
//
//    }];
}





@end
